<?php


namespace app\store\controller;
use app\wap\logic\wechat\WechatUtil;

class Mpapp extends Controller
{
    /**
     * 公众号设置
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function setting()
    {
        $model = db('wx_user')->find();
        if (!$this->request->isAjax()) {
            return $this->fetch('setting', compact('model'));
        }
    }
    public function huifu(){
        $model = db('wx_keyword')->find();
        if (!$this->request->isAjax()) {
            return $this->fetch('huifu', compact('model'));
        }
        if (db('wx_keyword')->insert($this->postData('article'))) {
            return $this->renderSuccess('添加成功', url('Mpapp/huifu'));
        }
        return $this->renderError('添加失败');
    }
    public function wxmenus(){
        $list = db('wx_menu')->where('pid',0)->select()->toArray();
        foreach ($list as $key => $val){
            $list[$key]['child'] = db('wx_menu')->where('pid',$val['id'])->select();
        }
        return $this->fetch('wxmenus', compact('list'));
    }
    public function add(){
        if (!$this->request->isAjax()) {
            $list = db('wx_menu')->where('pid',0)->select();
            return $this->fetch('add', compact('list'));
        }
        if (db('wx_menu')->insert($this->postData('category'))) {
            return $this->renderSuccess('添加成功', url('Mpapp/wxmenus'));
        }
        return $this->renderError( '添加失败');
    }
    public function edit($id){
        if (!$this->request->isAjax()) {
            $info = db('wx_menu')->where('id',$id)->find();
            $list = db('wx_menu')->where('pid',0)->select();
            return $this->fetch('edit', compact('list','info'));
        }
        if (db('wx_menu')->where('id',$id)->update($this->postData('category'))) {
            return $this->renderSuccess('修改成功', url('Mpapp/wxmenus'));
        }
        return $this->renderError( '修改失败');
    }
    public function del($id){
        if (db('wx_menu')->where('id',$id)->delete()) {
            return $this->renderSuccess('删除成功', url('Mpapp/wxmenus'));
        }
        return $this->renderError( '删除失败');
    }
    public function fabu(){
        //获取父级菜单
        $p_menus = db('wx_menu')->where(array('pid' => 0))->order('id ASC')->select();
        $p_menus = $this->convert_arr_key($p_menus, 'id');
        if (!count($p_menus) > 0) {
            return $this->renderError( '没有菜单可发布');
        }

        $post = $this->convert_menu($p_menus);
        $wechatObj = new WechatUtil();

        if ($wechatObj->createMenu($post) === false) {
            return $this->renderError( $wechatObj->getError());
        }
        return $this->renderSuccess( '发布成功！');
    }
    //菜单转换
    private function convert_menu($p_menus)
    {
        $new_arr = array();
        $count = 0;
        foreach($p_menus as $k => $v){
            $new_arr[$count]['name'] = $v['name'];

            //获取子菜单
            $c_menus = db('wx_menu')->where(['pid'=>$k])->select()->toArray();
            if($c_menus){
                foreach($c_menus as $kk=>$vv){
                    $add = array();
                    $add['name'] = $vv['name'];
                    $add['type'] = $vv['type'];
                    // click类型
                    if($add['type'] == 'click'){
                        $add['key'] = $vv['value'];
                    }elseif($add['type'] == 'view'){
                        $add['url'] = $vv['value'];
                    }elseif($add['type'] == 'miniprogram'){
                        $add['url'] = 'http://www.suiguantun.com';
                        $add['appid'] = $vv['appid'];
                        $add['pagepath'] = $vv['value'];
                    }else{
                        $add['key'] = $vv['value'];
                    }
                    if($add['name']){
//                        $add['sub_button'] = array();
                        $new_arr[$count]['sub_button'][] = $add;
                    }
                }
            }else{
                $new_arr[$count]['type'] = $v['type'];
                // click类型
                if($new_arr[$count]['type'] == 'click'){
                    $new_arr[$count]['key'] = $v['value'];
                }elseif($new_arr[$count]['type'] == 'view'){
                    //跳转URL类型
                    $new_arr[$count]['url'] = $v['value'];
                }elseif($new_arr[$count]['type'] == 'miniprogram'){
                    $new_arr[$count]['url'] = 'http://www.suiguantun.com';
                    $new_arr[$count]['appid'] = $v['appid'];
                    $new_arr[$count]['pagepath'] = $v['value'];
                }else{
                    //其他事件类型
                    $new_arr[$count]['key'] = $v['value'];
                }
            }
            $count++;
        }
        return array('button'=>$new_arr);
    }
    /**
     * @param $arr
     * @param $key_name
     * @return array
     * 将数据库中查出的列表以指定的 id 作为数组的键名
     */
    function convert_arr_key($arr, $key_name)
    {
        $arr2 = array();
        foreach($arr as $key => $val){
            $arr2[$val[$key_name]] = $val;
        }
        return $arr2;
    }
    public function huoma(){
        $list = db('wx_huoma')->order('id desc')->select();
        return $this->fetch('huoma', compact('list'));
    }
    public function hm_add(){
        if (!$this->request->isAjax()) {
            return $this->fetch('hm_add');
        }
        if (db('wx_huoma')->insert($this->postData('hm'))) {
            return $this->renderSuccess('添加成功', url('Mpapp/huoma'));
        }
        return $this->renderError( '添加失败');
    }
    public function hm_edit($id){
        if (!$this->request->isAjax()) {
            $info = db('wx_huoma')->where('id',$id)->find();
            $fileInfo = db('upload_file')->where(['file_id'=>$info['url']])->find();
            $info['image'] = $fileInfo['file_url'].'/'.$fileInfo['file_name'];
            return $this->fetch('hm_edit', compact('info'));
        }
        if (false !== db('wx_huoma')->where('id',$id)->update($this->postData('hm'))) {
            return $this->renderSuccess('修改成功', url('Mpapp/huoma'));
        }
        return $this->renderError( '修改失败');
    }
}